package com.tencent.qplus.paddata.util;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author ZhangXiang
 * 2010-8-10
 */
public class ConfigUtil {

	protected static final Properties props = new Properties();
	private static Logger logger = Logger.getLogger(ConfigUtil.class);
	static {
		try {
			InputStream in = ConfigUtil.class.getResourceAsStream("/config.properties");
			props.load(in);
			in.close();
		} catch (Exception ex) {
			logger.error(ex);
		}
	}
	
	public static Properties getOneProperties(String filePath){
		try {
			InputStream in = ConfigUtil.class.getResourceAsStream(filePath);
			Properties properties = new Properties();
			properties.load(in);
			in.close();
			return properties;
		} catch (Exception ex) {
			logger.error(ex);
		}
		return null;
	}
	
	
	public static String getSeverKey(){
		return ConfigUtil.getOneProperties("/serverkey.properties").getProperty("this_server_key");
	}
	/**
	 * 邮件列表
	 * @return
	 * @author ZhangXiang
	 * 2010-8-10
	 */
	public static MailPerson[] getMailRecipientList(String configPath){
		String mail_recipient = props.getProperty(configPath);
		String[] list = mail_recipient.split(";");
		int len = list.length;
		MailPerson[] ps = new MailPerson[len];
		int k=0;
		for(int i=0;i<len;i++){
			if(list[i].trim().length()>0){
				String[] ss = list[i].split(":");
				if(ss.length==2)
					ps[k++] = new MailPerson(ss[0],ss[1]);
			}
		}
		if(k<len){
			MailPerson[] ps2 = new MailPerson[k-1];
			System.arraycopy(ps, 0, ps2, 0, k-1);
			return ps2;
		}else{
			return ps;
		}
	}
	
	
	/**
	 * 邮件发送者
	 * @return
	 * @author ZhangXiang
	 * 2010-8-10
	 */
	public static MailPerson getMSender(){
		String mail_sender = props.getProperty("mail_sender");
		String[] ss = mail_sender.split(":");
		return new MailPerson(ss[0],ss[1]);
	}
	
	public static String getPath(String path){
		return props.getProperty(path);
	}

	public static int getInt(String key){
		return Integer.valueOf(props.getProperty(key));
	}
	public static String getString(String key){
		return props.getProperty(key);
	}
	public static float getFloat(String key){
		return Float.valueOf(props.getProperty(key));
	}
	public static Double getDouble(String key){
		return Double.valueOf(props.getProperty(key));
	}
}
